package com.avic.rong.adapter.collect;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CollectionActivity;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.bean.collect.StandardCollectBean;
import com.avic.rong.userOrder.api.AddCarApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StandardAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private ArrayList<StandardCollectBean> mList;

    private int position;

    public StandardAdapter(Context mContext, ArrayList<StandardCollectBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_standard, viewGroup, false);
        return new StandardHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        final StandardCollectBean bean = mList.get(i);
        StandardHolder standardHolder = (StandardHolder) viewHolder;
        standardHolder.tv_serial.setText(bean.getGoodsSn());
        standardHolder.tv_name.setText(bean.getGoodsName());
        DecimalFormat df = new DecimalFormat("#####0.00");
        standardHolder.tv_price.setText("¥"+df.format(bean.getGoodsPrice()));

        String valid = bean.getValid();
        if (!StringUtil.isEmpty(valid)){
            standardHolder.tv_status.setText(valid);
            standardHolder.tv_status.setVisibility(View.VISIBLE);
        }else {
            standardHolder.tv_status.setVisibility(View.INVISIBLE);
        }

        if (StringUtil.isEmpty(bean.getPublishDate())){
            standardHolder.tv_release_date.setVisibility(View.INVISIBLE);
        }else {
            standardHolder.tv_release_date.setVisibility(View.VISIBLE);
            standardHolder.tv_release_date.setText("发布日期："+bean.getPublishDate());
        }
        if (StringUtil.isEmpty(bean.getImplementationDate())){
            standardHolder.tv_material_date.setVisibility(View.INVISIBLE);
        }else {
            standardHolder.tv_material_date.setVisibility(View.VISIBLE);
            standardHolder.tv_material_date.setText("实施日期：" + bean.getImplementationDate());
        }
        //取消收藏
        standardHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (standButtonClick!=null){
                    standButtonClick.onClick(bean,i);
                }
            }
        });
       /* //添加购物车
        standardHolder.img_shop_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String skuStr = "{"+"\"" +bean.getTargetId()+"\""+":"+"1}";
                addCar(skuStr);
            }
        });*/

        //点击条目进详情
        standardHolder.llStandard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(DetailProductActivity.getIntent(mContext,bean.getTargetId()+""));
            }
        });

    }
    //添加购物车
    private void addCar(String skuStr) {
        Log.e("cjx", "skuStr:" + skuStr);
        if (!SharedPreferencesUtil.getLoginToken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", 4 + "");
//            是否再次购买（0/1）
            map.put("buyAgain", 0 + "");
            map.put("token", SharedPreferencesUtil.getLoginToken());
            map.put("deviceId", DeviceUtils.getIMEString(mContext));
            final AddCarApi addCarApi = new AddCarApi((CollectionActivity)mContext, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        }
    }

    //添加购物车
    HttpOnNextListener simpleOnNextListenerAddCar = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showToast(mContext,subjects.getMsg());
                return;
            }
            ToastTools.showToast(mContext, "添加成功");
        }

        @Override
        public void onCacheNext(String cache) {

        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class StandardHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ll_standard)
        LinearLayout llStandard;
        @BindView(R.id.tv_serial)
        TextView tv_serial;
        @BindView(R.id.tv_name)
        TextView tv_name;
       /* @BindView(R.id.img_shop_car)
        ImageView img_shop_car;*/
        @BindView(R.id.tv_material_date)
        TextView tv_material_date;
        @BindView(R.id.tv_release_date)
        TextView tv_release_date;
        @BindView(R.id.tv_price)
        TextView tv_price;
        @BindView(R.id.tv_status)
        TextView tv_status;

        @BindView(R.id.collect_btn_delete)
        Button btn_delete;

        public StandardHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            
        }
    }

    private StandButtonClick standButtonClick;

    public void setStandButtonClick(StandButtonClick standButtonClick) {
        this.standButtonClick = standButtonClick;
    }

    public interface StandButtonClick{
        void onClick(StandardCollectBean bean,int position);
    }

}
